#!/bin/bash
# GTK主题支持安装脚本
# 让 NotePad-- 完美融入 GNOME/GTK 桌面环境

set -e

echo "================================="
echo "NotePad-- GTK主题支持安装脚本"
echo "================================="
echo ""

# 检测桌面环境
DESKTOP_ENV=$(echo "$XDG_CURRENT_DESKTOP" | tr '[:upper:]' '[:lower:]')
echo "检测到桌面环境: $DESKTOP_ENV"
echo ""

# 检测Linux发行版
if [ -f /etc/os-release ]; then
    . /etc/os-release
    DISTRO=$ID
    DISTRO_VERSION=$VERSION_ID
    echo "发行版: $NAME $VERSION"
else
    echo "无法检测Linux发行版"
    exit 1
fi

# 根据发行版和桌面环境安装相应包
case $DISTRO in
    ubuntu|debian|linuxmint|pop)
        echo ""
        echo "开始安装 GTK 主题支持包..."
        echo ""
        
        if [[ "$DESKTOP_ENV" == *"gnome"* ]]; then
            echo "安装 GNOME 主题集成..."
            sudo apt update
            sudo apt install -y qgnomeplatform-qt5 adwaita-qt qt5-style-plugins
        elif [[ "$DESKTOP_ENV" == *"kde"* ]]; then
            echo "KDE 桌面已自带 Qt 主题支持，无需额外安装"
        else
            echo "安装 GTK 主题支持..."
            sudo apt update
            sudo apt install -y qt5-style-plugins qt5ct
        fi
        ;;
        
    fedora|rhel|centos)
        echo ""
        echo "开始安装 GTK 主题支持包..."
        echo ""
        
        if [[ "$DESKTOP_ENV" == *"gnome"* ]]; then
            echo "安装 GNOME 主题集成..."
            sudo dnf install -y qgnomeplatform-qt5 adwaita-qt5 qt5-qtstyleplugins
        elif [[ "$DESKTOP_ENV" == *"kde"* ]]; then
            echo "KDE 桌面已自带 Qt 主题支持，无需额外安装"
        else
            echo "安装 GTK 主题支持..."
            sudo dnf install -y qt5-qtstyleplugins qt5ct
        fi
        ;;
        
    arch|manjaro)
        echo ""
        echo "开始安装 GTK 主题支持包..."
        echo ""
        
        if [[ "$DESKTOP_ENV" == *"gnome"* ]]; then
            echo "安装 GNOME 主题集成..."
            sudo pacman -S --noconfirm qgnomeplatform-qt5 adwaita-qt5
            echo "安装 qt5-styleplugins (需要AUR)..."
            if command -v yay &> /dev/null; then
                yay -S --noconfirm qt5-styleplugins
            elif command -v paru &> /dev/null; then
                paru -S --noconfirm qt5-styleplugins
            else
                echo "请手动安装 AUR 包: qt5-styleplugins"
            fi
        elif [[ "$DESKTOP_ENV" == *"kde"* ]]; then
            echo "KDE 桌面已自带 Qt 主题支持，无需额外安装"
        else
            echo "安装 GTK 主题支持..."
            sudo pacman -S --noconfirm qt5ct
        fi
        ;;
        
    *)
        echo "不支持的发行版: $DISTRO"
        echo "请参考文档手动安装: docs/GTK_THEME_INTEGRATION.md"
        exit 1
        ;;
esac

echo ""
echo "================================="
echo "安装完成！"
echo "================================="
echo ""
echo "下一步："
echo ""

if [[ "$DESKTOP_ENV" == *"gnome"* ]]; then
    echo "1. 重新登录桌面会话（或重启电脑）"
    echo "2. 启动 NotePad--，应用会自动使用 GNOME 主题"
    echo "3. 查看终端输出确认："
    echo "   NotePad--"
    echo "   # 应显示: Setting Qt style to: gtk3"
    echo ""
    echo "如需手动配置，添加到 ~/.bashrc 或 ~/.zshrc："
    echo "   export QT_QPA_PLATFORMTHEME=gnome"
elif [[ "$DESKTOP_ENV" == *"kde"* ]]; then
    echo "KDE 用户无需额外配置，应用会自动使用 Plasma 主题"
else
    echo "1. 重新登录桌面会话"
    echo "2. 运行 qt5ct 配置工具："
    echo "   qt5ct"
    echo "3. 在界面中选择样式（推荐: GTK2 或 Fusion）"
    echo "4. 添加到 ~/.bashrc 或 ~/.zshrc："
    echo "   export QT_QPA_PLATFORMTHEME=qt5ct"
fi

echo ""
echo "详细文档: docs/GTK_THEME_INTEGRATION.md"
echo ""

